/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.scuecsz.model.bussiness;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.scuecsz.model.to.DIPLOMA;
import org.scuecsz.model.to.USER_MAIN;
import org.scuecsz.utils.ConnectionBuilder;
import org.scuecsz.utils.DBHandler;

/**
 *
 * @author Administrator
 */
public class AdminManageModel {
 public static String MAXPAGE="maxpage";
 public static String USERLIST="userlist";


    public static  Map queryUsers(List users,String condition,List values,int pageNum,int size) throws  Exception{
        String sql="select * from USER_MAIN where 1=1 "+(condition==null?"":condition);
        Connection conn=null;
        Map rs=new HashMap();
        try{
        conn = ConnectionBuilder.getConnection();
        int maxPage=DBHandler.getMaxPageNum(sql, values, size, conn);
        users=DBHandler.query("org.scuecsz.model.to.USER_MAIN", sql, values, users, pageNum, size, conn);
        rs.put(USERLIST, users);
        rs.put(MAXPAGE, maxPage);
        }catch(Exception e){
            throw e;
        }finally{
          ConnectionBuilder.closeConnection(conn);
        }
        return rs;
    }

public static int changeStatus(String status,String userid) throws Exception {
        int count=0;
        List values=new ArrayList();
        values.add(status);
        values.add(userid);
        Connection conn = null;
        try {
            conn = ConnectionBuilder.getConnection();
            count=DBHandler.executeUpdate("update USER_MAIN set status= ? where id=?", values, conn);
        } catch (Exception e) {
            throw e;
        } finally {
            ConnectionBuilder.closeConnection(conn);
        }
        return count;
    }

    public static int updateUser(USER_MAIN userMain) throws Exception {
        int count = 0;
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = ConnectionBuilder.getConnection();
            //插入校友基本数据到数据库
            count = DBHandler.update(userMain, conn);
        } catch (Exception e) {
            throw e;
        } finally {
            ConnectionBuilder.closePreparedStatement(pstmt);
            ConnectionBuilder.closeConnection(conn);
        }
        return count;
    }

    public static DIPLOMA getUserDiploma(String userid) throws Exception {
        DIPLOMA diploma=null;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs=null;
        String sql="SELECT * FROM diploma where USER_ID=?";
        try {
            conn = ConnectionBuilder.getConnection();
            //插入校友基本数据到数据库
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, userid);
            rs = pstmt.executeQuery();
            if(rs.next()){
                diploma = new DIPLOMA();
                diploma.setID(rs.getString("ID"));
                diploma.setUSER_ID(rs.getString("USER_ID"));
                diploma.setEXTENSION(rs.getString("EXTENSION"));
                diploma.setDIPLOMA(rs.getBytes("DIPLOMA"));
                diploma.setDATE(rs.getString("DATE"));
            }
        } catch (Exception e) {
            throw e;
        } finally {
            ConnectionBuilder.closeResultSet(rs);
            ConnectionBuilder.closePreparedStatement(pstmt);
            ConnectionBuilder.closeConnection(conn);
        }
        return diploma;
    }
}
